﻿<%@ Page Title="Stocks" Language="C#" MasterPageFile="~/Backoffice/BackOffice.Master"
    AutoEventWireup="true" CodeBehind="Stocks.aspx.cs" Inherits="TreaShares.Backoffice.User.Stocks" %>

<%@ MasterType VirtualPath="~/Backoffice/BackOffice.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHead" runat="server">
    <script type="text/javascript">
        $(function () {
            $("#btnStockInsert").click(function () {
                //debugger;
                var clientId = $("#lblOrganisationId").text();
                var stockName = $("#txtStockName").val();
                var ddlStockType = $("#ddlStockTypes").val();
                var ddlCurrency = $("#ddlCurrency").val();
                var issFVMultiplier = $("#txtIssFVMultiplier").val();
                var industryBurnRate = $("#txtIndustryBurnRate").val();

                var stockErrorList = $("#stockErrorList");
                stockErrorList.empty();
                var isValid = true;

                if (stockName == "") {
                    stockErrorList.append('<li>Stock Name cannot be empty</li>');
                    isValid = false;
                }
                if (issFVMultiplier != "") {
                    if (isNaN(issFVMultiplier)) {
                        stockErrorList.append('<li>Iss FV Multiplier is not a  valid number</li>');
                        isValid = false;
                    }
                    if (issFVMultiplier < 1) {
                        stockErrorList.append('<li>Iss FV Multiplier cannot be less then one</li>');
                        isValid = false;
                    }
                }
                if (industryBurnRate != "") {
                    if (isNaN(industryBurnRate)) {
                        stockErrorList.append('<li>Industry burn rate is not a valid number</li>');
                        isValid = false;
                    }
                    if (industryBurnRate < 0) {
                        stockErrorList.append('<li>Industry burn rate cannot be less then zero</li>');
                        isValid = false;
                    }
                }

                if (!isValid) {
                    return false;
                }
                //form1.submit();
                //var service = new TreaShares.Services.Stocks();
                //service.InsertStock(clientId, stockName, ddlStockType, ddlCurrency, paidUpCapital, issFVMultiplier, industryBurnRate, AjaxSucceeded, AjaxFailed, null);

            });
            $("#txtEffectiveDate").datepicker('setDate', new Date());
        });
        //        function AjaxSucceeded(result) {
        //            $("#lblStockMsg").text("Stock# " + result + " saved");
        //        }
        //        function AjaxFailed(result) {
        //            alert(result.status + ' ' + result.statusText);
        //        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="StockId" AutoGenerateColumns="False"
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="StockName" HeaderText="Name" />
            <asp:TemplateField HeaderText="Type">
                <ItemTemplate>
                    <%#Eval("StockCode")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlStockCode" runat="server" DataSourceID="ObjectDataSource3"
                        DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("StockCodeId") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:HyperLinkField DataNavigateUrlFields="StockId, StockName" DataNavigateUrlFormatString="StocksPaidUpCapital.aspx?StockID={0}&StockName={1}"
                HeaderText="Paid UP Capital" DataTextField="PaidUpCapitalDisplay"   />
            <asp:TemplateField HeaderText="Currency">
                <ItemTemplate>
                    <%#Eval("Currency") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlCurrency" runat="server" SelectedValue='<%# Bind("Currency") %>'>
                        <asp:ListItem Text="USD" Value="USD" />
                        <asp:ListItem Text="EUR" Value="EUR" />
                        <asp:ListItem Text="GBP" Value="GBP" />
                        <asp:ListItem Text="AUS" Value="AUS" />
                        <asp:ListItem Text="JPY" Value="JPY" />
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <%--<asp:BoundField DataField="PaidUpCapital" HeaderText="Paid Up Capital" DataFormatString="{0:N0}"
                ApplyFormatInEditMode="false" HtmlEncode="false">
                <ControlStyle Width="160" />
            </asp:BoundField>--%>
            <asp:BoundField DataField="IssFVMultiplier" HeaderText="ISS FV Multiplier" />
            <asp:BoundField DataField="IndustryBurnRate" HeaderText="Industry Burn Rate" />
            <asp:BoundField DataField="ParValue" HeaderText="Par Value" />
            <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Center">
                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center" />
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/Icons/trash.gif"
                        CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?')" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"
        InsertMethod="Insert" DeleteMethod="Delete" TypeName="TreaShares.Backoffice.Controlers.StocksManager"
        UpdateMethod="Update" >
        <SelectParameters>
            <asp:CookieParameter Name="clientId" CookieName="organisationId" Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="StockId" Type="Int32" />
            <asp:Parameter Name="stockName" Type="String" />
            <asp:Parameter Name="StockCode" Type="Int32" />
            <asp:Parameter Name="currency" Type="String" />
            <asp:Parameter Name="issFVMultiplier" Type="Double" />
            <asp:Parameter Name="industryBurnRate" Type="Double" />
            <asp:Parameter Name="ParValue" Type="Double" />
        </UpdateParameters>
        <DeleteParameters>
            <asp:Parameter Name="StockId" Type="Int32" />
        </DeleteParameters>
    </asp:ObjectDataSource>
    <br />
    <br />
    <table border="0" cellpadding="2" cellspacing="2">
        <tr>
            <td>
                Stock Name
            </td>
            <td>
                <input type="text" name="txtStockName" id="txtStockName" />
            </td>
            <td rowspan="7">
                <ul id="stockErrorList" class="AlertText">
                </ul>
            </td>
        </tr>
        <tr>
            <td>
                Stock Type
            </td>
            <td>
                <asp:DropDownList ID="ddlStockTypes" runat="server" DataSourceID="ObjectDataSource3"
                    DataTextField="Name" DataValueField="Id" Width="165px" ClientIDMode="Static">
                </asp:DropDownList>
                <asp:HyperLink ID="hlEntities" runat="server" NavigateUrl="~/Backoffice/User/StockTypes.aspx"
                    CssClass="editButton"></asp:HyperLink>
            </td>
        </tr>
        <tr>
            <td>
                Currency
            </td>
            <td>
                <asp:DropDownList ID="ddlCurrency" runat="server" Width="165px" ClientIDMode="Static">
                    <asp:ListItem Text="USD" Value="USD" />
                    <asp:ListItem Text="EUR" Value="EUR" />
                    <asp:ListItem Text="GBP" Value="GBP" />
                    <asp:ListItem Text="AUS" Value="AUS" />
                    <asp:ListItem Text="JPY" Value="JPY" />
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                ISS FV Multiplier
            </td>
            <td>
                <input type="text" name="txtIssFVMultiplier" id="txtIssFVMultiplier" />
            </td>
        </tr>
        <tr>
            <td>
                Industry Burn Rate
            </td>
            <td>
                <input type="text" name="txtIndustryBurnRate" id="txtIndustryBurnRate" />
            </td>
        </tr>
        <tr>
            <td>
                Par Value
            </td>
            <td>
                <input type="text" name="txtParValue" id="txtParValue" value="0" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <span id="lblStockMsg" class="AlertText"></span>
                <input type="reset" name="" id="btnReset" value="Reset" class="button" />
                <asp:Button Text="Insert" name="btnStockInsert" ID="btnStockInsert" runat="server"
                    class="button" OnClick="btnStockInsert_Click" ClientIDMode="Static" />
            </td>
        </tr>
    </table>
    <asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="SelectStockTypes"
        TypeName="TreaShares.Backoffice.Controlers.StocksManager">
        <SelectParameters>
            <asp:CookieParameter Name="clientId" CookieName="organisationId" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="SelectByStockId"
        TypeName="TreaShares.Backoffice.Controlers.StocksManager" InsertMethod="Insert">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="stockId" PropertyName="SelectedValue"
                Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
</asp:Content>
